﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using DevComponents.DotNetBar;
using shopsoft.main.AppCommon;
using shopsoft.models;
using shopsoft.models.EnumObject;
using System.Threading;
using shopsoft.main.CustomSearch;
using Elike.Toolkit.ControlsErgodic.Windows;
using shopsoft.business.SystemConfig;
using shopsoft.models.ClassBase;
using DevComponents.DotNetBar.Controls;

namespace shopsoft.main.MemberManage
{
    public partial class MemberInfo : Office2007Form
    {
        #region Private Attribute
        
        private WfControl wf = new WfControl();
        List<PermissionType> permissionTypes = new List<PermissionType>();
        private int checkedCount = 0;
        private MemberSearch searchF = new MemberSearch();
        private SCMember scMember = new SCMember();
        
        #endregion

        #region Structure Method

        /// <summary>
        /// 构造函数
        /// </summary>
        public MemberInfo()
        {
            InitializeComponent();
            Icon = Properties.Resources.UserManage16;
        }

        #endregion Structure Method

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MemberInfo_Load(object sender, EventArgs e)
        {
            searchF.Owner = this;

            dgvMember.AutoGenerateColumns = false;
            wf.SetDoubleBuffered(dgvMember, true);

            searchF.Owner = this;
            
            permissionTypes = new List<PermissionType> { 
                new PermissionType{
                   EPermissionType = EPermissionType.Select
                },
                new PermissionType{
                   EPermissionType = EPermissionType.Insert
                },
                new PermissionType{
                   EPermissionType = EPermissionType.Update
                },
                new PermissionType{
                   EPermissionType = EPermissionType.Delete
                },
                new PermissionType{
                   EPermissionType = EPermissionType.Recover
                }
            };

            wf.SetOperationItemVisible(permissionTypes, ',', dgvMember);
        }

        #region /*****************Method*****************/

        /// <summary>
        /// 长时间操作需要显示等待窗体
        /// </summary>
        public void LongTimeDoSomething()
        {
            ImageView iv = new ImageView(ClientParameterSettings.LoadingFlash) { StartPosition = FormStartPosition.CenterScreen };
            Thread thread = new Thread(ShowLoading) { IsBackground = true };
            thread.Start(iv);

            //数据处理函数耗时比较长
            Search();

            wf.SetAllControlsVisible(this, true);

            iv.Invoke((EventHandler)delegate { iv.Close(); });
        }

        /// <summary>
        /// 长时间操作需要显示等待窗体
        /// </summary>
        private void ShowLoading(object objForm)
        {
            ImageView iv = objForm as ImageView;
            if (iv != null)
                Application.Run(iv);
        }

        /// <summary>
        /// 查询会员信息
        /// </summary>
        public void Search()
        {
            //可以换行
            dgvMember.Columns["Store_Name"].CellTemplate.Style.WrapMode = DataGridViewTriState.True;

            List<Member> uMembers = new List<Member>();

            Member uMember = new Member() { /*修改默认查询条件中对删除记录状态的判断,false为查询未删除的*/DelFlag = false, CurrentOperator = Program.CurrentUser };

            if (searchF.where != null)
            {
                uMember.DelFlag = searchF.where.isDeleted;

                if (searchF.where.MemberGradeID != null)
                {
                    uMember.MemberGrade = new MemberGrade() { ID = searchF.where.MemberGradeID };
                }

                uMember.MemberNo = searchF.where.KeyWords[0];
                uMember.WorkUnit = searchF.where.KeyWords[0];

                uMember.DateTimeIntervals = searchF.where.DateTimeIntervals;
            }

            try
            {
                uMembers = scMember.GetMemberInfos(uMember);
                uMembers = uMembers.OrderBy(p => p.DelFlag).ThenByDescending(p => p.ID).ToList();


                List<DataRecord> dataRecords = new List<DataRecord>();
                foreach (var ucMember in uMembers)
                {
                    dataRecords.Add(ucMember);
                }
                page1.SetDataSource(dataRecords);
                page1.PageSize = ClientParameterSettings.DefaultPageSize;
                page1.ShowData();
            }
            catch (Exception ex)
            {
                MsgBox.Information("提示", ex.Message);
            }
        }

        #endregion /*****************Method*****************/

        #region /*****************Event*****************/

        /// <summary>
        /// 窗体加载完毕后，加载数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MemberInfo_Shown(object sender, EventArgs e)
        {
            //
            LongTimeDoSomething();
        }

        #endregion /*****************Event*****************/

        /// <summary>
        /// 新增事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonItem2_Click(object sender, EventArgs e)
        {
            try
            {
                MemberAu au = new MemberAu() { /*居中显示*/StartPosition = FormStartPosition.CenterParent };

                if (au.ShowDialog() == DialogResult.OK)
                {
                    Search();
                }
            }
            catch (Exception ex)
            {
                MsgBox.Information("提示", ex.Message);
            }
        }

        /// <summary>
        /// 查询事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonItem1_Click(object sender, EventArgs e)
        {
            //
            searchF.Show(sender as BaseItem, true);
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dgvMember_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            //
            DataGridViewX dataGridViewX = (DataGridViewX)sender;
            var models = (List<Member>)dataGridViewX.DataSource;
            
            for (int i = 0; i < models.Count; i++)
            {
                dataGridViewX.Rows[i].Cells["Member_ID"].Value = models[i].ID;
                dataGridViewX.Rows[i].Cells["Company_Name"].Value = models[i].Company.Name;
                dataGridViewX.Rows[i].Cells["Store_Name"].Value = models[i].Store.Name;
                dataGridViewX.Rows[i].Cells["Member_No"].Value = models[i].MemberNo;
                dataGridViewX.Rows[i].Cells["Member_Name"].Value = models[i].Name;
                dataGridViewX.Rows[i].Cells["Sex"].Value = models[i].Sex == true ? "男":"女";
                dataGridViewX.Rows[i].Cells["Mobile_No"].Value = models[i].MobilePhone;
                dataGridViewX.Rows[i].Cells["Balance"].Value = models[i].MemberBalance;
                dataGridViewX.Rows[i].Cells["Leaving_Deal_time"].Value = models[i].LeavingDealtime;
                dataGridViewX.Rows[i].Cells["Bouns"].Value = models[i].MemberBouns;

                dataGridViewX.Rows[i].Cells["btnUpdate"].Value = models[i].OperatorNo.ID != Program.CurrentUser.ID ||
                                                                 models[i].DelFlag == true
                                                                     ? Properties.Resources.Blank
                                                                     : Properties.Resources.Update16;
                
                dataGridViewX.Rows[i].Cells["btnPhoto"].Value = models[i].PhotoCount == 0
                                                                    ? Properties.Resources.PictureAdd16
                                                                    : Properties.Resources.Picture16;

                if (models[i].DelFlag == true)
                {
                    //改变被已删除数据的字体颜色
                    dataGridViewX.Rows[i].DefaultCellStyle.ForeColor = ClientParameterSettings.DeleledItemForeColor;

                    //修改
                    dataGridViewX.Rows[i].Cells["btnUpdate"].Value = Properties.Resources.Blank;
                    //照片
                    dataGridViewX.Rows[i].Cells["btnPhoto"].Value = Properties.Resources.Blank;
                }
            }
        }

        /// <summary>
        /// 翻页事件
        /// </summary>
        /// <param name="e"></param>
        private void page1_PageChanged(UserControls.PageControl.PageChangedEventArgs e)
        {
            //
            List<Member> members = new List<Member>();
            foreach (var dataRecord in e.List)
            {
                members.Add((Member)dataRecord);
            }
            dgvMember.DataSource = members;
            wf.SolveCreateHandleError(dgvMember);
            
            checkedCount = 0;
            
            bar1.Items["buttonItem" + ((int)EPermissionType.Delete).ToString()].Enabled = false;
            bar1.Items["buttonItem" + ((int)EPermissionType.Recover).ToString()].Enabled = false;
        }
    }
}
